﻿

using HBase; 
using System;
using System.Collections.Generic;
using System.Security.Cryptography;

namespace hconnect {
    public abstract class ScanSynchro : TestableTask {
      
        protected bool addUp = false;
        protected bool begin = false;
        protected ScanStatus status = null;
        protected String errorCode;
        public int t;
        public ScanSynchro(String errorCode = "NoRead") {
         
            this.errorCode = errorCode;

        }

        public void beginScan() {
            if (!begin) {
                status = null;
                t = 0;
                begin = true;
            }

        }

        public bool scaning() {
            return begin;
        }
        public void reScan() {
            status = null;
            t = 0;
            begin = true;
        }

        public void stopScan() {
            status = null;
            t = 0;
            begin = false;
        }
        public virtual bool isOutOffTime() {
            return t > maxTime() && status == null;
        }
        public virtual bool isReaded() {
            return status != null;
        }

        public String getCode() {
            if (status != null&& status.code.Count>0) {
                return status.code[0];
            }
            return null;
        }
        public List<String> getAllCode() {
            if (status != null) {
                return status.code;
            }
            return null;

        }
        protected virtual int maxTime() {
            return 100000;
        }
        protected virtual void addSpan() {
           
            t++;
        }

        public override bool excute() {
            if (!begin) {
                return true;
            }
            if (status!=null&& !addUp) {
                return true;
            }
            addSpan();


            if (StatusCache.test) {
                status = (ScanStatus)textParse(status);
            } else {
                ScanStatus n= read();
                if (status == null || !addUp) {
                    status = n;
                } else {
                    List<String> list = status.code;
                    foreach (String code in n.code) {
                        if (!list.Contains(code)) {
                            list.Add(code);
                        }
                    }
                   
                }
               
            }

            beforeSync(status);
             extralSleep();
            return true;
        }

       protected virtual void extralSleep() {
            oneTimeSleep = t * 50;
            if (oneTimeSleep > 1000) {
                oneTimeSleep = 1000;
            }
            
        }

        public abstract ScanStatus read();
        protected override bool onlyOneTimeAsync() {
         
            time = 100;
            return true;
        }
    }
}
